//
//  UniPartyParser.m
//  RegBars
//
//  Created by Huu Nhat on 9/26/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "UniPartyParser.h"

#define PARTY_NODE @"party"
#define ID_NODE @"id"
#define TIME_NODE @"time"
#define NAME_NODE @"name"
#define THUMBNAIL_NODE @"thumbnail"

@implementation UniPartyParser
@synthesize currentParty;
@synthesize delegate;
@synthesize currentString;
@synthesize partyArray;

- (void) getPartysFromURL:(NSString*) URL withDelegate:(id) theDelegate{
	self.delegate = theDelegate;
	partyArray = [[NSMutableArray alloc] init];
	responseData = [[NSMutableData data] retain];
	NSURL *baseURL = [NSURL URLWithString:[[URL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] retain]];
	
	NSURLRequest *request = [NSURLRequest requestWithURL:baseURL];
	
	[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [responseData setLength:0];
	
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"RegBar cannot load data at this time, please try again later" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
    [errorAlert release];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
	NSXMLParser *rssParser = [[NSXMLParser alloc] initWithData:responseData];
	currentString = [[NSMutableString alloc] init];
	[rssParser setDelegate:self];
	
	[rssParser parse];
}

#pragma mark rssParser methods

- (void)parserDidStartDocument:(NSXMLParser *)parser {
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
    if ([elementName isEqualToString:PARTY_NODE]){
		currentParty = [[Party alloc] init];
	}
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
	
	NSString *string = [NSString stringWithFormat:@"%@",currentString];
	if ([elementName isEqualToString:PARTY_NODE]) {
		[partyArray addObject:currentParty];
		[currentParty release];
	}
	if ([elementName isEqualToString:ID_NODE]) {
		[currentParty setPartyId:string];
	}
	if ([elementName isEqualToString:TIME_NODE]) {
		[currentParty setTime:string];
	}
	if ([elementName isEqualToString:NAME_NODE]) {
		[currentParty setName:string];
	}
	if ([elementName isEqualToString:THUMBNAIL_NODE]) {
		string = [string stringByReplacingOccurrencesOfString:@"\n" withString:@""];
		string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
		[currentParty setThumbnailUrl:[string stringByReplacingOccurrencesOfString:@"\n" withString:@""]];
	}
	[self.currentString setString:@""];
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
	[self.currentString appendString:string];
}

- (void)parserDidEndDocument:(NSXMLParser *)parser
{
	if ([delegate respondsToSelector:@selector(didEndGetParty:)])
	{		
		[delegate didEndGetParty:partyArray];
		[partyArray removeAllObjects];
		[partyArray release];
	}
    else
	{ 
		return;
		[NSException raise:NSInternalInconsistencyException
					format:@"Delegate doesn't respond to receivedNews:"];
	}
}

- (void) dealloc{
	[currentString release];
	[super dealloc];
}

@end
